package com.yehun.utils;

import com.yehun.config.SpringMvcActionContext;
import com.yehun.enums.BaseEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Genius on 2017/4/1 0001.
 */
public class ParamUtils {

    private static Logger logger = LoggerFactory.getLogger(ParamUtils.class);

    public static Map<String, Object> getParameter() {
        HttpServletRequest request = SpringMvcActionContext.getRequest();
        Map<String, Object> conditions = new HashMap<>();
        Map map = request.getParameterMap();
        if(!map.isEmpty()) {
            for (Object o : map.keySet()) {
                String key = (String) o;
                conditions.put(key, ((String[]) map.get(key))[0]);
            }
        }
        return conditions;
    }

    public static boolean exists(String name) {
        HttpServletRequest request = SpringMvcActionContext.getRequest();
        return request.getParameterMap().containsKey(name);
    }

    public static String getString(String name) {
        HttpServletRequest request = SpringMvcActionContext.getRequest();
        Map<String, String[]> map = request.getParameterMap();
        String ret = null;
        if(map.containsKey(name)) {
            ret = request.getParameter(name);
        } else {
            Object tempValue = request.getAttribute(name);
            if(tempValue != null) {
                ret = tempValue.toString();
            }
        }
        logger.info("parameter => " + name + " => " + ret);
        return ret;
    }

    public static int getPageIndex() {
        return getPageIndex(Constant.Parameter.PageIndex);
    }

    public static int getPageIndex(String name) {
        int value = getInt(name);
        if (value <= 0) value = 1;
        return value;
    }

    public static int getPageIndex(String name, int pagesize, int total) {
        int value = getPageIndex(name);
        int pagenum = total % pagesize == 0 ? total / pagesize : total / pagesize + 1;
        if (value > pagenum) value = 1;
        return value;
    }

    public static int getPageSize() {
        return getPageSize(Constant.Parameter.PageSize, Constant.Default.PageSize);
    }

    public static int getPageSize(String name, int defaultValue) {
        int value = getInt(name);
        if (value == 0 || value == Constant.Default.ValueInt) {
            return defaultValue;
        }
        return value;
    }

    public static int getInt(String name) {
        int value = Constant.Default.ValueInt;
        try {
            String param = getString(name);
            value = Integer.parseInt(param);
        } catch (NumberFormatException e) {
            logger.error(e.getMessage());
        }
        return value;
    }

    public static int getInt(String name, int value) {
        try {
            String param = getString(name);
            if(!StringUtils.isEmpty(param))
                value = Integer.parseInt(param);
        } catch (NumberFormatException e) {
            logger.error(e.getMessage());
        }
        return value;
    }

    public static short getShort(String name) {
        short value = Constant.Default.ValueShort;
        try {
            String param = getString(name);
            value = Short.parseShort(param);
        } catch (NumberFormatException e) {
            logger.error(e.getMessage());
        }
        return value;
    }

    public static short getShort(String name, short value) {
        try {
            String param = getString(name);
            if(!StringUtils.isEmpty(param))
                value = Short.parseShort(param);
        } catch (NumberFormatException e) {
            logger.error(e.getMessage());
        }
        return value;
    }

    public static long getLong(String name) {
        long value = Constant.Default.ValueLong;
        try {
            String param = getString(name);
            value = Long.parseLong(param);
        } catch (NumberFormatException e) {
            logger.error(e.getMessage());
        }
        return value;
    }

    public static long getLong(String name, long value) {
        try {
            String param = getString(name);
            if(!StringUtils.isEmpty(param))
                value = Long.parseLong(param);
        } catch (NumberFormatException e) {
            logger.error(e.getMessage());
        }
        return value;
    }

    public static float getFloat(String name) {
        float value = Constant.Default.ValueFloat;
        try {
            String param = getString(name);
            value = Float.parseFloat(param);
        } catch (NumberFormatException e) {
            logger.error(e.getMessage());
        }
        return value;
    }

    public static float getFloat(String name, float value) {
        try {
            String param = getString(name);
            if(!StringUtils.isEmpty(param))
                value = Float.parseFloat(param);
        } catch (NumberFormatException e) {
            logger.error(e.getMessage());
        }
        return value;
    }

    public static boolean getBoolean(String name) {
        boolean value = false;
        try {
            String param = getString(name);
            value = Boolean.parseBoolean(param);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return value;
    }

    @SuppressWarnings("deprecation")
    public static Date getDate(String name, Date date) {
        Date value = date;
        try {
            String param = getString(name);
            value = new Date(Date.parse(param));
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return value;
    }

    public static Integer[] getIntArray(String name, String split) {
        try {
            String param = getString(name);
            String[] array = param.split(split);
            List<String> list = Arrays.asList(array);
            Integer[] intArray = new Integer[list.size()];
            //list.forEach(m -> intArray.add(Integer.parseInt(m)));
            for (int i = 0; i < list.size(); i++) {
                intArray[i] = Integer.parseInt(list.get(i));
            }
            return intArray;
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return new Integer[]{};
    }

    public static Long[] getLongArray(String name, String split) {
        try {
            String param = getString(name);
            String[] array = param.split(split);
            List<String> list = Arrays.asList(array);
            Long[] longArray = new Long[list.size()];
            //list.forEach(m -> intArray.add(Integer.parseInt(m)));
            for (int i = 0; i < list.size(); i++) {
                longArray[i] = Long.parseLong(list.get(i));
            }
            return longArray;
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return new Long[]{};
    }

    public static String[] getStringArray(String name, String split) {
        try {
            String param = getString(name);
            return param.split(split);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return new String[]{};
    }

    public static <T extends BaseEnum> T getEnum(String name, Class<T> t, T value) {
        int id = ParamUtils.getInt(name);
        T temp = EnumUtils.getEnum(t, id);
        if(temp != null) {
            value = temp;
        }
        return value;
    }
}
